//
//  PictureViewController.m
//  News
//
//  Created by xalo on 16/2/26.
//  Copyright © 2016年 News. All rights reserved.
//

#import "PictureViewController.h"


@interface PictureViewController ()<UIScrollViewDelegate>

@property (strong, nonatomic) IBOutlet UISegmentedControl *selelction;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;


@end

@implementation PictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//
    //
    _scrollView.delegate =self;
    
    [self setSelelctioN ];
    
    [self.view addSubview:_scrollView];
   

}



-(void)setSelelctioN {
    
    
    _selelction.tintColor = [UIColor clearColor];
    
    CGFloat width = self.view.frame.size.width;

    self.selelction.frame = CGRectMake(width/6, 20, (2*width)/3, 44);


    
    [_selelction addTarget:self action:@selector(segmentValueChange) forControlEvents:(UIControlEventValueChanged)];
    
    //设置字体
    NSDictionary *norDic = @{NSForegroundColorAttributeName :[UIColor blackColor]};
    [_selelction setTitleTextAttributes:norDic forState:UIControlStateNormal];
    
    
    NSDictionary *seleDic = @{NSForegroundColorAttributeName :[UIColor redColor  ]};
    [_selelction setTitleTextAttributes:seleDic forState:UIControlStateSelected ];
    
    
    [self.selelction setBackgroundImage:[UIImage imageNamed:@"underline_selected"] forState:(UIControlStateSelected) barMetrics:(UIBarMetricsDefault)];
    
    [self.selelction setBackgroundImage:[UIImage imageNamed:@"underline_Unselected"] forState:(UIControlStateNormal) barMetrics:(UIBarMetricsDefault)];

    
    [self.view addSubview:_selelction ];
    
    
}



//通过选择下标,
-(void)segmentValueChange {
    
   
    NSInteger index = [_selelction selectedSegmentIndex];
    CGFloat width = _scrollView.frame.size.width ;
    
    _scrollView.contentOffset = CGPointMake(index * width, 0);
    
}
//通过滑动选择上边下标

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat width = scrollView.frame.size.width ;
    
    NSInteger num = _scrollView.contentOffset.x/ width ;
    
    _selelction.selectedSegmentIndex = num;
    
    
}









@end
